package com.netty.esc.service.active.impl;

import com.netty.esc.common.enums.WSProtocolTypeEM;
import com.netty.esc.entity.business.UserAndChannelDTO;
import com.netty.esc.entity.send.FriendADDPushDTO;
import com.netty.esc.entity.send.FriendOnlindDTO;
import com.netty.esc.service.active.FriendsActiveAction;
import com.netty.esc.config.websocket.ChannelGroups;
import com.netty.esc.config.websocket.WebSocketManager;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ext.zhaoyang17
 * @date: 2022.11.04
 * 好友行为类
 */
@Service
public class FriendsActiveActionImpl implements FriendsActiveAction {


    public void friendRequest(Long userId){
        //通知客户端要拉取服务端信息了
        Channel channel = ChannelGroups.getChannelByUerId(userId);
        FriendADDPushDTO friendADDPushDTO = new FriendADDPushDTO();
        WebSocketManager.sendTextMessage(channel,friendADDPushDTO);
    }

    @Override
    public void assentFriendOnline(List<Long> userList) {
        List<UserAndChannelDTO> channelListByUserId = ChannelGroups.getChannelListByUserId(userList);
        for (UserAndChannelDTO userAndChannelDTO : channelListByUserId) {
            FriendOnlindDTO friendOnline = new FriendOnlindDTO();
            friendOnline.setProtocolType(WSProtocolTypeEM.FRIEND_ONLINE.code());
            friendOnline.setUserId(userAndChannelDTO.getUserId());
            WebSocketManager.sendTextMessage(userAndChannelDTO.getChannel(),friendOnline);
        }
    }

    @Override
    public void assentFriendDown(List<Long> userList) {
        List<UserAndChannelDTO> channelListByUserId = ChannelGroups.getChannelListByUserId(userList);
        for (UserAndChannelDTO userAndChannelDTO : channelListByUserId) {
            FriendOnlindDTO friendOnline = new FriendOnlindDTO();
            friendOnline.setProtocolType(WSProtocolTypeEM.FRIEND_DOWN.code());
            friendOnline.setUserId(userAndChannelDTO.getUserId());
            WebSocketManager.sendTextMessage(userAndChannelDTO.getChannel(),friendOnline);
        }
    }
}
